<?php
if(isset($_POST['subject'])){
	require_once('client_config.php');
	require_once('ntml_soapclient.php');

	$client = new MyNTMLSoapClient('./ews/services.wsdl', $soap_options);
	$client->set_profile($user_profile['login'], $user_profile['password']);
	$appointment = new Appointment();
	$appointment->set_subject($_POST['subject']);
	$appointment->set_desc($_POST['description']);
	$appointment->set_location($_POST['location']);
	$appointment->set_categories(explode(',',$_POST['categories']));
	$appointment->set_start_datetime($_POST['startdate'] . 'T' . $_POST["starttime"]);
	$appointment->set_end_datetime($_POST['enddate'] . 'T' . $_POST["endtime"]);
	$id = $client->add_appointment($appointment, $errcode, $error);
	if($errcode === "NoError"){
		header('Location: ' . dirname($_SERVER['PHP_SELF']) .'/get_appointment.php?Id=' . urlencode($id));
	}else{
		echo "Error: " . $errcode . "( " . $error ." )";
	}
}else{
	$form = "
	<form method='post' action='add_appointment.php'>
	<table align='center'>
		<caption><font color='blue'><b>Add new appointment</b></caption>
		<tr><td>Subject: </td><td colspan='2'><input id='subject' type='text' name='subject'/></td></tr>
		<tr><td>Description:</td><td colspan='2'><textarea rows='10' cols='60' id='description' name='description'></textarea></td></tr>
		<tr><td>Location: </td><td colspan='2'><input id='location' type='text' name='location'/></td></tr>
		<tr><td>Categories: </td><td colspan='2'><input id='categories' type='text' name='categories'/> (Separate categories by comma)</td></tr>
		<tr><td>Start date/time:</td><td> <input id='date' type ='text' name='startdate'/>YYYY-MM-DD</td> <td><input id='time' type ='text' name='starttime'/>HH-Mi-SS</td></tr>
		<tr><td>End date/time: </td><td><input id='date' type ='text' name='enddate'/>YYYY-MM-DD</td><td><input id='time' type ='text' name='endtime'/>HH-Mi-SS</td></tr>
		<tr><td colspan='3' align='center'><input type='submit' value='Add'></td></tr>
	</table>
	</form>";
	echo $form;
}
?>